import {
	request
} from '@/api/request/index.js'
import share from '@/components/share.vue'
import cartapi from '@/api/cart/model.js'

export default {
	data() {
		return {
			cartList: [],
			recommendList: [],
			total: 0,
			totalPrice: 0,
			totaldicount: 0,
			checkall: false,
			baseres:this.$resBaseurl
		}
	},
	onShow() {
		this.init()
	},
	onLoad:function(){
		// #ifdef MP-WEIXIN
		uni.hideShareMenu()
		// #endif
	},
	methods: {
		init() {
			this.totalPrice = 0;
			this.totaldicount = 0;
			this.total = 0;
			this.checkall = false;

			let list = cartapi.getCartList()
			this.cartList = list
			let totalprice = 0,
				totaldicount = 0,
				total = 0,
				checkall = true;
			if(list.length==0)checkall = false;
			list.map((item, idx) => {
				item.Title = item.IsFlash ? '支持闪送' : '不支持闪送';
				item.img=this.$cartImgsrc.replace('{0}',item.CommodityName)
				if (item.Checked) {
					totalprice += parseFloat(((item.ZkPrice || item.Price) * item.Num).toFixed(2));
					totaldicount += item.Discount;
					total += item.Num;
				} else checkall = false;
			})
			this.totalPrice = totalprice
			this.totaldicount = totaldicount
			this.total = total
			this.checkall = checkall
		},
		minusitem(cart) {
			let go = () => {
				cartapi.modify('reduce', cart)
				this.init()
			}
			if (cart.Num <= 1) {
				uni.showModal({
					title: '提示',
					content: `是否将${cart.CommodityName}从购物车移除？`,
					success(r) {
						if (r.confirm) {
							go()
						}
					}
				})
			} else go()

		},
		plusitem(cart) {
			cartapi.modify('add', cart)
			this.init()
		},
		choose(item, type,$event) {
			if (type) {
				if(this.cartList.length==0)return;
				this.checkall = !this.checkall;
				let chackall = this.checkall
				this.cartList.map((sitem) => {
					sitem.Checked = chackall
					cartapi.modify('choose', sitem)
				})
			} else {
				item.Checked = !item.Checked
				cartapi.modify('choose', item)
			}

			this.init()
		},
		clearall($event) {
			let that=this
			if (!this.checkall) return;
			uni.showModal({
				title: '提示',
				content: `是否清空当前城市的购物车商品？`,
				success(r) {
					if (r.confirm) {
						cartapi.modify('clearall')
						that.init()
					}
				}
			})
		},
		//防止冒泡和滚动穿透
		stopPrevent() {},
		gohref(item){
			let href = "/pages/detail/detail?cid="+item.CommodityId+"&pid="+item.PackageId;
			uni.navigateTo({
				url:href
			})
		},
		goOrder() {
			//先判断是否登录
			let wanttoUrlCopy = '/pages/confirm-order/confirm-order'
			let jumpUrl = ''
			// #ifdef H5
			jumpUrl = '/pages/login/common'
			// #endif
			// #ifndef H5
			jumpUrl = '/pages/login/quick'
			// #endif
			this.$checkLoginState.checkloginstate(() => {
				uni.navigateTo({
					url: '../../pages/confirm-order/confirm-order'
				})
			}, () => {
				uni.navigateTo({
					url: jumpUrl+'?redirect=' + wanttoUrlCopy
				})
			});
		}
	}
};
